package com.itheima.controller;


import com.github.pagehelper.PageInfo;
import com.itheima.domain.UserInfo;
import com.itheima.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {


    @Autowired
    private IUserService userService;


    @RequestMapping("findAll.do")
    public ModelAndView findAll(@RequestParam(name = "page") int page, @RequestParam(name = "pageSize") int pageSize){
        ModelAndView mv=new ModelAndView();
        List<UserInfo> userList= userService.findAll(page,pageSize);
        PageInfo pageInfo=new PageInfo(userList);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("user-list");
        return mv;
    }

    @RequestMapping("save.do")
    public String saveUser(UserInfo userInfo){
        userService.saveUser(userInfo);
        return "redirect:findAll.do?page=1&pageSize=4";
    }
}
